module PostWindow
  class Post
    def initialize
      @window = Gtk::Window.new
      @window.title = 'Make a Post'
      @window.resizable = true
      @window.keep_above = false
      @window.icon_name = Gtk::Stock::ADD
      @window.set_frame_dimensions(12, 12, 12, 12)
      @window.set_default_size(400, 300)
    
      vbox = Gtk::VBox.new(false, 0)
      @window.add(vbox)
    
      table = Gtk::Table.new(2, 3)
      table.row_spacings = 6
      vbox.pack_start(table)
    
      title_area_label = Gtk::Label.new('_Title')
      title_area_label.use_underline = true
      table.attach(title_area_label, 0, 1, 0, 1, Gtk::FILL, xpad = 6)
      @title_entry = Gtk::Entry.new
      table.attach(@title_entry, 1, 2, 0, 1)
      title_area_label.set_mnemonic_widget(@title_entry)
    
      body_area_label = Gtk::Label.new('_Body')
      body_area_label.use_underline = true
      table.attach(body_area_label, 0, 1, 2, 3, Gtk::FILL, xpad = 6)
      body_area_window = Gtk::ScrolledWindow.new
      body_area_window.shadow_type = Gtk::SHADOW_IN
      body_area_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
      @buffer = Gtk::TextBuffer.new
      window_entry = Gtk::TextView.new(@buffer)
      window_entry.set_wrap_mode(Gtk::TextTag::WRAP_WORD)
      body_area_window.add(window_entry)
      table.attach(body_area_window, 1, 2, 2, 4)
      body_area_label.set_mnemonic_widget(window_entry)
      window_entry.accepts_tab = false
      
      buttonbox = Gtk::HButtonBox.new
      buttonbox.layout_style = Gtk::ButtonBox::DEFAULT_STYLE
      buttonbox.spacing = 6
      vbox.add(buttonbox)
    
      reset_button = Gtk::Button.new(Gtk::Stock::CLEAR)
      reset_image = Gtk::Image.new(Gtk::Stock::CLEAR, Gtk::IconSize::BUTTON)
      reset_button.set_image(reset_image)
      reset_label = Gtk::Label.new('Reset')
      reset_button.set_label("Reset")
      reset_button.signal_connect('clicked') { reset }
      buttonbox.add(reset_button)
    
      cancel_button = Gtk::Button.new(Gtk::Stock::CANCEL)
      cancel_image = Gtk::Image.new(Gtk::Stock::CANCEL, Gtk::IconSize::BUTTON)
      cancel_button.set_image(cancel_image)
      cancel_button.signal_connect('clicked') { @window.hide }
      buttonbox.add(cancel_button)
    
      post_button = Gtk::Button.new(Gtk::Stock::ADD)
      post_image = Gtk::Image.new(Gtk::Stock::ADD, Gtk::IconSize::BUTTON)
      post_button.set_image(post_image)
      post_label = Gtk::Label.new('Post')
      post_button.set_label("Post")
      post_button.signal_connect('clicked') { post }
      buttonbox.add(post_button)
    
      @window.show_all
    end

    def reset
      @title_entry.set_text('')
      @buffer.set_text('')
    end

    def post
      user = USER
      pass = PASS
      title = @title_entry.text
      parser = RedCloth.new @buffer.text
      body = parser.to_html
    
      if title == '' or body == ''
        puts "Title and/or body are empty."
        @window.hide
      end
    
    
      blog = GData::Blogger.new(BLOG_ID)
      blog.authenticate(user, pass)
      blog.post_entry(title, body)
    
      @window.hide
    end
  end
end
